package com.shj.web.security.service;

import com.shj.model.SysUser;
import com.shj.service.SysUserService;
import com.shj.web.security.entity.SelfUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @Description TODO SpringSecurity用户的业务实现
 * @Author songhuajian
 * @Date 2020/3/13 0:18
 **/
@Component
public class SelfUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    /**
     * @description 查询用户信息
     * @author songhuajian
     * @date 2020/3/13 0:19
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.login(username);
        if (sysUser != null){
            SelfUserEntity selfUserEntity = new SelfUserEntity();
            selfUserEntity.setUserId(sysUser.getUserId());
            selfUserEntity.setUsername(sysUser.getUsername());
            selfUserEntity.setPassword(sysUser.getPassword());
            selfUserEntity.setStatus(sysUser.getStatus().toString());
            return selfUserEntity;
        }
        return null;
    }
}
